﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_UI;
using System;
using System.Collections;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;


namespace STHPMS1.Web.W_Controller.Basis
{
    [RoutePrefix("api/roadnet")]
    public class B_RoadnetController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetroadnetByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string road = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            //Expression<Func<B_Roadnet, bool>> exp = null;
            //if (road != null && road.ToString().Length > 0)
            //    exp = pre => pre.Rdwayid == road.ToString();

            var data = this.Scene.Bll.B_Roadnet.Get<B_Roadnet>(null, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("")]
        public HttpResponseMessage Post(B_Roadnet entity)
        {
            if (entity.Roadnet_Id == null)
            {
                entity.Roadnet_Id = MyApp.Locator.GetInstance<IKeyGenrateService>().NewGuid();
                var data = this.Scene.Bll.B_Roadnet.Get<B_Roadnet>(pre => pre.Roadnet_Name == entity.Roadnet_Name, null).ToList < B_Roadnet>();
                if (data.Count>=1)
                {
                    HttpContext.Current.Response.Write("路网名称已存在");
                    HttpContext.Current.Response.End();
                    return null;
                }
            }
            else
            {
                var data = this.Scene.Bll.B_Roadnet.Get<B_Roadnet>(pre => pre.Roadnet_Name == entity.Roadnet_Name && pre.Roadnet_Id != entity.Roadnet_Id, null).ToList<B_Roadnet>();
                if (data.Count>=1)
                {
                    HttpContext.Current.Response.Write("路网名称已存在");
                    HttpContext.Current.Response.End();
                    return null;
                }
            }

            this.Scene.Bll.B_Roadnet.Save(entity);
            

            var res = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetroadnetByPage", new { page = 1 });
            res.Headers.Location = new Uri(uri);

            return res;
        }

        [Route("")]
        public void Delete(B_Roadnet entity)
        {
            this.Scene.Bll.B_Roadnet.Remove(entity);
        }


        /// <summary>
        /// 获取所有路网 
        /// </summary>
        /// <returns></returns>
        [Route("getroadnet")]
        public dynamic GetRoadnet()
        {
            var innerJoinQuery =
                from road in this.Scene.Bll.B_Roadnet.Get<B_Roadnet>(null,null)
                select new { id = road.Roadnet_Id, text = road.Roadnet_Name };
            return innerJoinQuery;
        }

    }
}